package com.recruit.wechat.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.recruit.wechat.bean.Permission;
import com.recruit.wechat.bean.result.ResultPage;
import com.recruit.wechat.bean.result.TreeModel;
import com.recruit.wechat.controller.helper.$;
import com.recruit.wechat.controller.model.ApiResultModel;
import com.recruit.wechat.mapper.PermissionMapper;
import com.recruit.wechat.service.PermissionService;

import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单资源表 服务实现类
 * </p>
 *
 * @author 肖守凯
 * @since 2019-09-03
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    /** 添加Permission
     * @param permission  前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel addPermission(Permission permission  ) {
           if(this.save(permission )){
               return $.success();
           }else{
                return $.error("保存失败");
           }
    }
    /** 编辑跳转页面的回显数据Permission
     * @param id  前端传进的参数
     * @return 返回实体类
     */
    @Override
    public Permission selectPermissionByIdPermission(Integer id ){
            return this.getById(id);
    }


    /** 编辑Permission
     * @param permission  前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel editPermission(Permission permission  ) {
           if(this.updateById(permission )){
               return $.success();
           }else{
               return $.error("编辑失败");
           }
    }
    /** 删除一条Permission
     * @param id  前端传进的ID
     * @return 返回统一信息
     */
    @Override
    public   ApiResultModel delPermission( Integer id  ){
           if(this.removeById(id)){
               return $.success();
           }else{
               return $.error("删除失败");
           }
    }

   /** 分页查询Permission
     * @param page  页码
     * @param limit  查几条
     * @param permission  前端传进的参数
     * @return 返回统一分页信息
     */
    @Override
    public ResultPage findPermissionByPage(Integer page, Integer limit, Permission permission){
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page", (page - 1) * limit);
        paramMap.put("limit", limit);
        paramMap.put("permission", permission);
        List<Permission> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<Permission> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public List<TreeModel> finPermissionAjax() {
        return baseMapper.finPermissionAjax();
    }
}
